package com.huasan.www.demon.rest.resource;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.ws.rs.Consumes;
import javax.ws.rs.FormParam;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

import org.glassfish.jersey.server.mvc.Viewable;
import org.springframework.beans.factory.annotation.Autowired;

import com.huasan.www.demon.data.service.SetinfoService;
import com.huasan.www.demon.pojo.Setinfo;
import com.huasan.www.demon.pojo.SetinfoExample;
import com.huasan.www.demon.util.DataTableUtil;
import com.huasan.www.demon.util.OnePage;

@Path("/set")
public class SetInfoResource extends Resource{
	@Autowired
	private SetinfoService setinfoService;
	
	@GET
	@Produces("text/html")
	public Viewable getSet(){
		return getView("set", null);
	}
	
	@GET
	@Path("/mset")
	@Produces("text/html")
	public Viewable getMSet(){
		Map<String, Object> map = new HashMap<String, Object>();
		try{
			SetinfoExample example = new SetinfoExample();
			SetinfoExample.Criteria criteria =   example.createCriteria();
			criteria.andExpiredEqualTo("0");
			List<Setinfo> list = setinfoService.selectByExample(example);
			map.put("sets", objectmapper.writeValueAsString(list));
			return getView("mset", null, map);
		}catch (Exception e){
			logger.error(e.getMessage(), e);
			return getView("mset", null, map);
		} 
	}
	
	@GET
	@Path("/allsets")
	@Produces({MediaType.APPLICATION_JSON} )
	public String getAllSets() {
		try{
			SetinfoExample example = new SetinfoExample();
			SetinfoExample.Criteria criteria =   example.createCriteria();
			criteria.andExpiredEqualTo("0");
			List<Setinfo> list = setinfoService.selectByExample(example);
			return this.getSucReturn(objectmapper.writeValueAsString(list), "");
		}catch (IOException e){
			logger.error(e.getMessage(), e);
			return getErrReturn("Get sets error");
		} 
	}
	
	@POST
	@Path("/sets")
	@Produces({MediaType.APPLICATION_JSON} )
	@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
	public String getSets(@FormParam("sEcho") String sEcho,
			@FormParam("iDisplayStart") Integer iDisplayStart,
			@FormParam("iDisplayLength") Integer iDisplayLength,
			@FormParam("sSearch") String sSearch,
			@FormParam("title") String title
			) {
		try{
			SetinfoExample example = new SetinfoExample();
			SetinfoExample.Criteria criteria =   example.createCriteria();
			criteria.andExpiredEqualTo("0");
			if (title!=null){
				criteria.andTitleLike(DataTableUtil.wrapLike( title ));
			}
			Integer total = setinfoService.countByExample(example);
			example.setLimitStart(iDisplayStart);
			example.setLimitEnd(iDisplayLength);
			OnePage<Setinfo> page = setinfoService.selectOnePageByExample(example);
			return DataTableUtil.getDataTable(sEcho, total, total
					, objectmapper.writeValueAsString(page.getDataList()));
		}catch (IOException e){
			logger.error(e.getMessage(), e);
			return getErrReturn("Get sets error");
		} 
	}
}
